﻿using ZYCommon;
using System;
using System.Drawing;

namespace TextEditor
{


    public class ZYAttribute
    {
        public object DefaultValue = null;
        private object objValue = null;
        private string strName = null;

        public ZYAttribute Clone()
        {
            ZYAttribute attribute = new ZYAttribute();
            attribute.Name = this.strName;
            attribute.objValue = this.objValue;
            attribute.DefaultValue = this.DefaultValue;
            return attribute;
        }

        public bool EqualsValue(ZYAttribute myAttr)
        {
            if (myAttr == null)
            {
                return false;
            }
            if (myAttr == this)
            {
                return true;
            }
            if (myAttr.Name != this.strName)
            {
                return false;
            }
            if (this.Value != null)
            {
                return this.Value.Equals(myAttr.Value);
            }
            return (myAttr.Value == null);
        }

        /// <summary>
        /// 获取默认属性
        /// </summary>
        /// <param name="strName"></param>
        /// <returns></returns>
        public static object GetDefaultValue(string strName)
        {
            switch (strName)
            {
                case "fontname":
                    return "宋体";
                case "fontsize":
                    return 12f;
                case "fontbold":
                    return false;
                case "fontitalic":
                    return false;
                case "bed_no":
                    return "";
                case "section_name":
                    return "";
                case "sick_area":
                    return "";
                case "fontunderline":
                    return false;
                case "format":
                    return "";
                case "cell-style":
                    return "border:lrtb";
                case "skipcreate":
                    return false;
                case "lineSpacing":
                    return 10;
                case "paragraphSpacing":
                    return 10;
                case "skip":
                    return false;
                case "leftchars":
                    return 0;
                case "printVisible":
                    return false;
                case "min-height":
                    return 0;
                case "border-style":
                    return 0;
                case "forecolor":
                    return Color.Black;
                case "sub":
                    return false;
                case "sup":
                    return false;
                case "tableLock":
                    return false;
                case "visible":
                    return false;
                case "candelete":
                    return false;
                case "deleter":
                    return -1;
                case "creator":
                    return 0;
                case "savelist":
                    return true;
                case "align":
                    return 0;
                case "source":
                    return "0000000000";
                case "listsource":
                    return "0";
                case "operatercreater":
                    return 0;
                case "sign":
                    return "";
                case "saveinfile":
                    return false;
                case "multiple":
                    return false;
                case "hidetitle":
                    return false;
                case "height":
                    return 1;
                case "nocontent":
                    return false;
                case "titleline":
                    return false;
                case "linecolor":
                    return Color.Black;
                case "linewidth":
                    return 1;
                case "check":
                    return "N";
            }
            return null;
        }

        public bool isNeedSave()
        {
            if (this.objValue == null)
            {
                return false;
            }
            if (this.objValue.Equals(this.DefaultValue))
            {
                return false;
            }
            if (this.objValue is bool)
            {
                return (bool)this.objValue;
            }
            return true;
        }

        internal void SetValue(object v)
        {
            this.objValue = v;
        }

        public string Name
        {
            get
            {
                return this.strName;
            }
            set
            {
                this.strName = value;
                this.DefaultValue = GetDefaultValue(this.strName);
            }
        }

        public object Value
        {
            get
            {
                if (this.objValue == null)
                {
                    return this.DefaultValue;
                }
                return this.objValue;
            }
            set
            {
                this.objValue = value;
            }
        }

        public string ValueString
        {
            get
            {
                if (this.DefaultValue is bool)
                {
                    if ((bool)this.objValue)
                    {
                        return "1";
                    }
                    return null;
                }
                if (this.DefaultValue is Color)
                {
                    return StringCommon.ColorToHtml((Color)this.objValue);
                }
                if (this.objValue == null)
                {
                    return null;
                }
                return this.objValue.ToString();
            }
            set
            {
                if (this.DefaultValue is bool)
                {
                    this.objValue = value != null;
                }
                else if (this.DefaultValue is Color)
                {
                    this.objValue = StringCommon.ColorFromHtml(value, Color.Black);
                }
                else if (this.DefaultValue is ParagraphAlignConst)
                {
                    this.objValue = (ParagraphAlignConst)StringCommon.ToInt32Value(value, (int)this.DefaultValue);
                }
                else if (this.DefaultValue is int)
                {
                    this.objValue = StringCommon.ToInt32Value(value, (int)this.DefaultValue);
                }
                else if (this.DefaultValue is float)
                {
                    this.objValue = Convert.ToSingle(value);
                }
                else
                {
                    this.objValue = value;
                }
            }
        }
    }
}

